package com.zret.oasys.operation.dept;
/*
 * 通过部门关键字模糊查询部门
 */
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.zret.oasys.entity.Dept;

public class GetDeptByDeptName implements DeptServletOperation {

	@Override
	public void doOperation(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException 
	{
		//1编码
		response.setContentType("application/json;charset=utf-8");
		//2从jsp获取deptname，获取查询到的list
		String name = request.getParameter("deptname");
		
		//如果是传过来的是空值判断
		if ("".equals(name))
		{
			//如果获取空值则查询所有部门
			List<Dept> list = deptservice.getDepts();
			//3创建输出流
			PrintWriter out = response.getWriter();
			//4创建Gson 对象
			Gson gson = new Gson();
			//5将查询的部门list转为字符串
			String jsoString = gson.toJson(list);
			//6控制台测试打印
			System.out.println(jsoString);
			//7传输给jsp页面
			out.print(jsoString);
			//8关闭out
			out.close();
		}
		else
		{
			//模糊查询部门的方法
			List<Dept> list = deptservice.getDeptByDeptName(name);
			//3创建输出流
			PrintWriter out = response.getWriter();
			//4创建Gson对象
			Gson gson = new Gson();
			//5将查询到的部门list转为字符串
			String jsonString = gson.toJson(list);
			//6控制台测试打印输出
			System.out.println(jsonString);
			//7传输给jsp页面
			out.print(jsonString);
			//8关闭out
			out.close();
		}
	}
}
